#!/bin/bash
export BASE_DIR=`cd $(dirname $0); pwd`
cd $BASE_DIR

# 杀死并等待执行完毕, 第一个参数进程id, 第二个参数等待次数
kill_and_wait(){
	kill -15 $1
	c=0
	while kill -0 $1 2> /dev/null;
	do
		sleep 0.5;
		((c=$c+1))
		if [ $c == $2 ]; then
			echo "wait too many times. will kill processor id: $1"
			kill -9 $1
			fi
	done;
}

for file in $(ls *.jar)
do
  pid=`ps -ef|grep $file|grep -v grep|awk '{print $2}'`
  if [ ! -n "$pid" ]; then
    echo "$file already stopped."
  else
    echo "$file is running. pid: $pid"
    kill_and_wait $pid 30
  fi
done
